layui.use(['form','jquery','layer'], function () {
    var $ = layui.jquery,
        form = layui.form,
        layer = layui.layer;

    //定义常量
    var submit_btn = {
        is_lock: false,
    };

    //弹出层index
    var tcindex;

   //请求前
    submit_btn.ajax_before = function () {
        submit_btn .is_lock = true;
        tcindex = layer.msg('正在处理 ...', {
            shade: 0.3
            ,time: false
        });
    }
    //请求完成
    submit_btn.ajax_complete = function () {
        submit_btn .is_lock = false;
        layer.close(tcindex)
    }

    //通用提交  适用于新建 与 修改
    window.ajaxs = function (url,data,successfunc,errorfunc) {

        //检测是否在请求中
        if (submit_btn.is_lock == true) {
            layer.msg("正在处理中",'warning');
            return false;
        }

        //提交
        $.ajax({
            beforeSend: submit_btn.ajax_before,
            type: 'POST',
            url: url,
            data: data,
            dataType: 'json',
            success: function (data) {
                if(data.Code == 200){
                    successfunc(data);
                } else {
                    errorfunc(data);
                }
            },
            complete: submit_btn.ajax_complete
        });
    }

    //使用form对象提交
    window.ajaxForm = function (url,data,successfunc,errorfunc) {

        //检测是否在请求中
        if (submit_btn.is_lock == true) {
            layer.msg("正在处理中",'warning');
            return false;
        }

        //提交
        $.ajax({
            beforeSend: submit_btn.ajax_before,
            type: 'POST',
            url: url,
            data: data,
            dataType: 'json',
            contentType : false,
            processData : false,
            success: function (data) {
                if(data.Code == 200){
                    successfunc(data);
                } else {
                    errorfunc(data);
                }
            },
            complete: submit_btn.ajax_complete
        });
    }

})
